package geektime.spring.springbucks.service;

import geektime.spring.springbucks.controller.request.CoffeeRequest;
import geektime.spring.springbucks.mapper.CoffeeMapper;
import geektime.spring.springbucks.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.exact;

@Slf4j
@Service
public class CoffeeServiceImpl implements CoffeeService {
    @Autowired
    private CoffeeMapper coffeeMapper;

    /**
     * 分页查询数据，page：查询页数，size：每页数据条数
     */
    public List<Coffee> findAllCoffeeByPage(int page, int size) {
        List<Coffee> coffee = coffeeMapper.findAllWithRowBounds(new RowBounds(page, size));
        return coffee;
    }

    /**
     * 根据主键批量查询，id：主键
     */
    public List<Coffee> findCoffeeByListID(List<Long> idList) {
        List<Coffee> coffee = coffeeMapper.findCoffeeByListID(idList);
        return coffee;
    }

    public List<Coffee> getAllCoffee(){
        List<Coffee> coffee = coffeeMapper.getAllCoffee();
        return coffee;
    }

    public Coffee findCoffeeByName(String name) {
        Coffee coffee = coffeeMapper.findCoffeeByName(name);
        return coffee;
    }

    public Coffee findCoffeeById(String id) {
        Coffee coffee = coffeeMapper.findCoffeeById(id);
        return coffee;
    }

    /**
     * 插入一条数据
     */
    @Override
    @Transactional
    public void addCoffee(String name, long price) {
        coffeeMapper.addCoffee(name, price);
    }

    /**
     * 根据名称删除一条数据
     */
    @Override
    @Transactional
    public void delCoffeeByName(String name) {
        coffeeMapper.delCoffeeByName(name);
    }

    /**
     * 根据ID修改一条数据
     */
    @Override
    @Transactional
    public void updateCoffeeByID(String name, long price, long id) {
        coffeeMapper.updateCoffeeByID(name,price,id);
    }

    /**
     * 获取当前表条数
     */
    public long getCount() {
        long coffee = coffeeMapper.getCount();
        return coffee;
    }
}
